Repository navigation
fix: don't process Value names for backslash escapes in re.sub - #149
Merged
Merged
Conversation
A backslash in a Value name escaped the re.sub() replacement as a raw re.PatternError (e.g. 'Value beer\ol1 (\S+)'), instead of a TextFSMTemplateError when the value is used. A callable replacement keeps the name literal. Co-authored-by: rahul05ranjan <11793455+rahul05ranjan@users.noreply.github.com>
harro
requested changes
Nov 16, 2026
Per review: a backslash in a Value name is now rejected at name-check
time, and the template is built by slicing the already-validated leading
'(' instead of re.sub() with a callable replacement. Tests trimmed to
the name-check error and the full-template case.
Co-authored-by: rahul05ranjan <11793455+rahul05ranjan@users.noreply.github.com>
Contributor
Author
|
Thanks for the review! All suggestions applied in af91364.
Suite: 108 passed, 28 subtests passed ( |
harro
self-requested a review
November 16, 2026 02:48
harro
approved these changes
Nov 16, 2026
Contributor
Author
|
Thank you for the careful review and the merge @harro! The template-construction suggestion made the change much cleaner. On a personal note — I'm currently open to new opportunities (Full Stack (PHP, NodeJs, Python) / Data Science / Applied AI / Devtools, based in India). If there's an opening or referral you'd recommend, my inbox is open: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #148
Problem
A backslash in a
Valuename madeTextFSMValue.Parseraise a rawre.PatternError— unwrapped — because the name is interpolated into anre.sub()replacement string ((?P<%s> ...), whichre.subprocesses for backslash escapes (it would also expand group references such as\g<1>in a name). Every other malformed template raisesTextFSMTemplateError; the Value's own regex compilation is even wrapped withexcept re.error: raise TextFSMTemplateError(...)— thisre.subjust bypassed that wrapping.Fix
Use a callable replacement so the name is inserted literally:
Behavior for valid names is unchanged. A name that isn't a valid group name now surfaces later as the existing
TextFSMTemplateError("Invalid regular expression: ...") when the value is referenced in a rule, consistent with all other template errors.Testing
Added to
tests/textfsm_test.py(testFSMValue):Value beer\ol1 (\S+)now parses withtemplate == r'(?P<beer\ol1>\S+)'— fails onmainwith the rawre.PatternError: bad escape \o at position 8, passes with this fix.TextFSMTemplateErrorfromTextFSM, not a raw exception.Full suite:
python3 -m unittest tests.textfsm_test tests.clitable_test tests.terminal_test tests.texttable_test→Ran 108 tests ... OK(no failures before or after; the new assertions live inside the existingtestFSMValue).